#!/bin/bash

Upgrade_Nginx()
{

    Cur_Nginx_Version=`/usr/local/nginx/sbin/nginx -v 2>&1 | cut -c22-`

    if ! grep -Eqi "^#memc" /usr/local/nginx/conf/nginx.conf && /usr/local/nginx/sbin/nginx -V 2>&1 | grep -Eqi 'memc-nginx' ; then
        echo "Your Nginx with memc-nginx-module"
     else
        echo "Your Nginx without memc-nginx-module"
    fi

    Nginx_Version=""
    echo "Current Nginx Version:${Cur_Nginx_Version}"
    echo "You can get version number from http://nginx.org/en/download.html"
    read -p "Please enter nginx version you want, (example: ${Nginx_Version} ): " Nginx_Version
    if [ "${Nginx_Version}" = "" ]; then
        echo "Error: You must enter a nginx version!!"
        exit 1
    fi
    echo "+---------------------------------------------------------"
    echo "|    You will upgrade nginx version to ${Nginx_Version}"
    echo "+---------------------------------------------------------"

    Press_Start

    echo "============================check files=================================="
    cd ${cur_dir}/src
    [[ ! -s ${Openssl_Ver}.tar.gz ]] && wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate ${Download_Mirror}/openssl/${Openssl_Ver}.tar.gz
    [[ -d "${Openssl_Ver}" ]] && rm -rf ${Openssl_Ver}
    tar zxf ${Openssl_Ver}.tar.gz
    Nginx_With_Openssl="--with-openssl=${cur_dir}/src/${Openssl_Ver}"
    if [ -s nginx-${Nginx_Version}.tar.gz ]; then
        echo "nginx-${Nginx_Version}.tar.gz [found]"
     else
        echo "Error: nginx-${Nginx_Version}.tar.gz not found!!!download now......"
        wget -c ${Download_Mirror}/nginx/nginx-${Nginx_Version}.tar.gz
        if [ $? -eq 0 ]; then
            echo "Download nginx-${Nginx_Version}.tar.gz successfully!"
         else
            wget -c http://nginx.org/download/nginx-${Nginx_Version}.tar.gz
            if [ $? -eq 0 ]; then
                echo "Download nginx-${Nginx_Version}.tar.gz successfully!"
             else
                echo "You enter Nginx Version was:"${Nginx_Version}
                echo -e "\e[0;33mError! You entered a wrong version number, please check!\e[0m"
                sleep 5
                exit 1
            fi
        fi
    fi
    if ! grep -Eqi "^#memc" /usr/local/nginx/conf/nginx.conf && /usr/local/nginx/sbin/nginx -V 2>&1 | grep -Eqi 'memc-nginx' ; then
        cd ${cur_dir}/src
        rm -fr memc-nginx-module
        rm -fr srcache-nginx-module
        git clone -b master https://github.com/openresty/memc-nginx-module.git
        git clone -b master https://github.com/openresty/srcache-nginx-module.git
        Nginx_Modules_Memc="--add-module=../memc-nginx-module --add-module=../srcache-nginx-module"
        if [ ! -d srcache-nginx-module ]; then
            wget -c https://github.com/openresty/srcache-nginx-module/archive/master.zip -O srcache-nginx-module-master.zip
            wget -c https://github.com/openresty/memc-nginx-module/archive/master.zip -O memc-nginx-module-master.zip
            unzip -o memc-nginx-module-master.zip
            unzip -o srcache-nginx-module-master.zip
            Nginx_Modules_Memc="--add-module=../memc-nginx-module-master --add-module=../srcache-nginx-module-master"
        fi
        [[ ! -s /usr/local/memcached/bin/memcached ]] && Install_Memcached
    fi
    echo "============================check files=================================="
    tar zxf nginx-${Nginx_Version}.tar.gz
    cd nginx-${Nginx_Version}
    ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module ${Nginx_With_Openssl} ${NginxMAOpt} ${Nginx_Modules_Memc}
    make
    mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx-${Cur_Nginx_Version}.${Upgrade_Date}
    \cp objs/nginx /usr/local/nginx/sbin/nginx
    echo "Test nginx configure file..."
    /usr/local/nginx/sbin/nginx -t
    echo "upgrade..."
    make upgrade
    echo -e "   \e[0;32m======== upgrade nginx completed ======\e[0m"
    echo "Program will display Nginx Version......"
    /usr/local/nginx/sbin/nginx -v
    rm -fr ${cur_dir}/src/nginx-${Nginx_Version}
    rm -fr ${cur_dir}/src/memc-nginx-module*
    rm -fr ${cur_dir}/src/srcache-nginx-module*
    rm -fr ${cur_dir}/src/${Openssl_Ver}
}